package com.vandagroup.md5;
import java.security.SecureRandom;

import com.vanda.util.LogFunc;
public class Crypt {
	ByteStrCvr _cvr = new ByteStrCvr();
	byte[] passwd1 = null;
	byte[] passwd2 = null;
	SecureRandom rng = new SecureRandom();
	MD5password pw = new MD5password(rng);
	public static void main(String[] arg) {
		Crypt c = new Crypt();
		String _p1 = "A1!@#$%^&*";
		String _p2 = c.EnCrypt(_p1);
		LogFunc.info(_p2);
//		String _p2 = "1efeed9ddb0cfc622ebe6bd20aab7dbf5f9ec6f1aafc5f4d7a0b2b96b84da3bf0e60ec311efeed9d";
		boolean _b = c.CheckCrypt( _p2,_p1);
		LogFunc.info(new Boolean(_b));
	}
	public boolean CheckCrypt(String encrypttext, String plaintext) {
		try {
			passwd1 = _cvr.strToByte(encrypttext.substring(0, 32));
			passwd2 = _cvr.strToByte(encrypttext.substring(32, 72));
			if ((pw.VerifyHash(plaintext, passwd2, passwd1))) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			LogFunc.info(e.getMessage());
			return false;
		}
	}
	public String EnCrypt(String plaintext) {
		try {
			passwd1 = pw.GenerateHash(plaintext);
			String jcepw = _cvr.byteToStr(passwd1);
			passwd2 = pw.GetSalt();
			return (
				_cvr.byteToStr(passwd1)
					+ _cvr.byteToStr(passwd2)
					);
		} catch (Exception e) {
			LogFunc.info(e.getMessage());
			return ("");
		}
	}
}